<?php

namespace App\Admin\Controllers;

use App\Admin\Model\Users;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Widgets\Table;

class UserController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('用户列表');
            $content->description('description');

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('用户编辑');
            $content->description('description');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('新建用户');
            $content->description('description');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(Users::class, function (Grid $grid) {

            $grid->filter(function ($filter) {

                // 去掉默认的id过滤器
                // $filter->disableIdFilter();

                // 在这里添加字段过滤器
                $filter->like('nickname', '昵称');
                $filter->like('mobile', '手机号');
                $filter->like('sex', '性别');
            });
            $grid->uid('UID')->sortable()->orderBy('uid', 'desc');
            $grid->avatar_path('头像')->display(function ($avatar_path) {
                return '<img src=' . str_replace('raw.jpg', '50.jpg', $avatar_path) . '  alt="用户头像" style="width:50px"/>';
            });
            $grid->mobile('账号/手机号');
            $grid->nickname('昵称')->editable();
            $grid->point('积分')->editable();
            $grid->total_cost('总消费')->label('info');
            $grid->is_owner('是否房东')->display(function ($is_owner) {
                switch ($is_owner) {
                    case 1:
                        return '申请中.....';
                        break;
                    case 2:
                        return '是';
                        break;
                    case 3:
                        return '未通过';
                        break;

                    default:
                        return '可能是个会员！';
                        break;
                }
            })->label('info');
            $grid->last_login('上次登录时间')->display(function ($last_login){
                return date("Y-m-d H:i:s", $last_login);
            })->label('info');
            $grid->real_name('真实姓名')->editable();
            $grid->sex('性别')->editable();
            $grid->birthday('生日')->editable('date');
            // $grid->invitation_ceremony('粉丝数')->pluck('mobile')->label();
            $grid->rebate_balance('返利记录')->display(function ($rebate_balance) {
                // dd($rebate_balance);
                if($rebate_balance != ''){
                    return '<a href="table/' . $this->getKey() . '/rebate"><i class="fa fa-eye">查看</i></a>';
                }else{
                    return '<span ">无</span>';
                }
                
            })->label('info');
            $grid->invitation_ceremony('粉丝')->display(function ($invitation_ceremony) {
                if($invitation_ceremony == null) {
                    return  '<span class="label label-success ">'.count($invitation_ceremony).'</span>';
                }else{
                    // $profile = [];
                    // for($i=0;$i<count($invitation_ceremony);$i++){
                    //     $profile[$i] = array_only($invitation_ceremony[$i],['id','mobile','created_at']);
                    // }
                    // $table = new Table(['ID','手机号','邀请时间'], $profile);
                    // return $table->render();
                    return '<a href="table/' . $this->getKey() . '/invitation_ceremony"><i class="fa fa-eye">查看</i></a>';
                }
            },'查看');
            $grid->card_balance('余额')->editable();
            // $grid->card_balance();
            $grid->deadline('会籍过期时间')->display(function ($deadline) {
                return date("Y-m-d H:i:s", $deadline);
            })->editable();
            $grid->cash('提现总额')->display(function ($cash){
                $cash_amount=0.00;
                for($i=0;$i<count($cash);$i++){
                    $cash_amount = bcadd($cash_amount, $cash[$i]['cash_amount']);
                }
                // dd($cash);
                return '<a href="cash?&cid=&uid=' . $this->getKey() . '&owner_name=&owner_mobile=">'.$cash_amount. '</a>';
            })->label('info');
            $grid->created_at('创建时间');
            $grid->updated_at('更新时间');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        // return Admin::form(Users::class, function (Form $form) {

        //     $form->display('uid', 'uid');
        //     $form->display('mobile','账号/手机号');
        //     $form->display('password', '密码');
        //     $form->display('nickname', '昵称');
        //     $form->image('avatar_path','头像');
        //     $form->display('created_at', '创建时间');
        //     $form->display('updated_at', '更新时间');
        // });
        return Users::form(function(Form $form){
            $form->model()->makeVisible('password');
            $form->tab('基本信息', function (Form $form) {

                $form->display('uid');
                $form->image('avatar_path', '头像');
                $form->display('mobile', '账号/手机号');
                $form->display('nickname', '昵称');
                // $form->display('password', '密码');
                $form->display('real_name', '真实姓名');
                $form->radio('sex', '性别')->options(['man' => '男', 'woman' => '女'])->default('man');
                $form->radio('is_owner','是否是房东')->options([1 => '申请', 2 => '已通过',3=> '未通过'])->default(1);
                $form->date('birthday', '生日')->format('YYYY-MM-DD');
                $form->display('account_balance', '收入余额');
                $form->display('card_balance', '会员卡余额');
                $form->radio('banned', '是否禁用')->options([1 => '启用', 0 => '禁用']);
                $form->display('real_card', '账号身份证号');
                $form->email('email', '邮箱');
                $form->display('created_at', '创建时间');
                $form->display('updated_at', '更新时间');

            });
            $form->ignore(['password_confirmation']);
        });
    }

    public static function Fas()
    {
        // table 1
        $headers = ['Id', 'Email', 'Name', 'Company'];
        $rows = [
            [1, 'labore21@yahoo.com', 'Ms. Clotilde Gibson', 'Goodwin-Watsica'],
            [2, 'omnis.in@hotmail.com', 'Allie Kuhic', 'Murphy, Koepp and Morar'],
            [3, 'quia65@hotmail.com', 'Prof. Drew Heller', 'Kihn LLC'],
            [4, 'xet@yahoo.com', 'William Koss', 'Becker-Raynor'],
            [5, 'ipsa.aut@gmail.com', 'Ms. Antonietta Kozey Jr.'],
        ];

        $table = new Table($headers, $rows);

        echo $table->render();
        
    }

    public function delete(Request $request)
    {
        dd(Post::find($request->get('uid')));
    }
}
